package com.learn.io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @author wangxing
 * @version 2020/7/30 8:45 Administrator
 */
public class AioServer {
    public static void main(String[] args) throws IOException, InterruptedException {
//        创建管道
        final AsynchronousServerSocketChannel serverSocketChannel =
                AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(9090));
        serverSocketChannel.accept(null,
                new CompletionHandler<AsynchronousSocketChannel, Object>() {
                    /**
                     * Invoked when an operation has completed.
                     *
                     * @param
                     * @param attachment
                     */
                    @Override
                    public void completed(AsynchronousSocketChannel socketChannel, Object attachment) {
                        try {
                            System.out.println(socketChannel.getRemoteAddress());
                            serverSocketChannel.accept(attachment, this);
                            ByteBuffer buffer = ByteBuffer.allocate(1024);
                            socketChannel.read(buffer,buffer,new CompletionHandler<Integer,ByteBuffer>(){
                                /**
                                 * Invoked when an operation has completed.
                                 *
                                 * @param result     The result of the I/O operation.
                                 * @param attachment
                                 */
                                @Override
                                public void completed(Integer result, ByteBuffer attachment) {
                                    buffer.flip();
                                    System.out.println(new String(buffer.array(),0,result));
                                    socketChannel.write(ByteBuffer.wrap("hello client i am AioServer ".getBytes()));
                                }

                                /**
                                 * Invoked when an operation fails.
                                 *
                                 * @param exc        The exception to indicate why the I/O operation failed
                                 * @param attachment
                                 */
                                @Override
                                public void failed(Throwable exc, ByteBuffer attachment) {
                                    exc.printStackTrace();
                                }
                            });

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    /**
                     * Invoked when an operation fails.
                     *
                     * @param exc        The exception to indicate why the I/O operation failed
                     * @param attachment
                     */
                    @Override
                    public void failed(Throwable exc, Object attachment) {
                        exc.printStackTrace();
                    }
                });
        Thread.sleep(1000*60*10);
    }
}
